package action.photo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import model.Album;
import model.Photo;
import model.User;

import org.apache.struts2.ServletActionContext;

import DAO.PhotoDAO;
import DAO.DAOImple.PhotoImple;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PhotoAction extends ActionSupport {
	
	private Album album;
	private List<Album> albumList ;
	private Photo photo;
	private List<Photo> photoList;
	private List<Photo> photoListThumb;
	
	//private User user;
	
//	private Photo file;
	private List<File> upload;
	private List<String> uploadFileName;
	private List<String> uploadContentTypes;
	private String savePath;
	private int ableSize;
	private int size;
	
	public String addPhoto() {
		PhotoDAO photoDao = new PhotoImple();
		for(int i=0; i<upload.size(); i++) {
			System.out.println("i = " + i);
			
			
			uploadFile(i);
			
			photoDao.addPhoto(photo, album.getId());
		}
		photoDao.getAlbumList();
		return SUCCESS;
	}
	
	public String photoValidate() {
		
		ActionContext actionContext = ActionContext.getContext();
		String stringResult = "imgerror";
		setAbleSize(500 * 1024);
		String fileType = null;
//		upload = new ArrayList<File>();
		for(int i=0; i<upload.size(); i++) {
			System.out.println(upload);
			if(getUploadFileName() != null) {
				fileType = getUploadFileName().get(i).substring(getUploadFileName().lastIndexOf("."));
				Pattern p = Pattern.compile("jpg|gif|png");
				Matcher m = p.matcher(fileType);
				if(!m.matches()) {
					actionContext.put("wrong", "0");
				} else {
					boolean result = uploadFile(i);
					if(result) {
						photo = new Photo();
						photo.setPhotoName(getUploadFileName().get(i));
						photo.setPhotoPath("photo");
						photo.setSmallName("t_" + getUploadFileName().get(i));
						photo.setSmallPath("photo");
						
						
					} else {
						actionContext.put("wrong", "3");
						actionContext.put("fileSize", getSize());
						stringResult = "imgerror";
					}
				}
			} else {
				actionContext.put("wrong", "1");
				stringResult = "imgerror";
			}
			
		}
		return stringResult;
	}
	
	public boolean uploadFile(int i) {
		boolean result = false;
		setAbleSize(500 * 1024);
		if(getUploadFileName() != null) {
			//String fileName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + (int)(Math.random() * 4);
			String fileName = java.util.UUID.randomUUID().toString(); 
			String fileType = getUploadFileName().get(i).substring(getUploadFileName().get(i).lastIndexOf("."));
			String temp = fileName + fileType;
			System.out.println(getSavePath() + "\\" + getUploadFileName().get(i));
			
			photo = new Photo();
			photo.setPhotoName(temp);
//			photo.setSmallName("t_" + getUploadFileName().get(i));
			photo.setPhotoPath(getSavePath() + "\\" + temp);
			photo.setSmallPath(getSavePath() + "\\" + "t_" + temp);
			
			System.out.println(getSavePath() + "\\" + "t_" + temp);
			
			   
			try{
				System.out.println(upload.size());
				InputStream fileIn = new FileInputStream(upload.get(i));
				
				setSize(fileIn.available());
				if(getSize() <= getAbleSize()) {
					FileOutputStream fileOut = new FileOutputStream(getSavePath() + "\\" + temp);
					
					byte[] buffer = new byte[1024];
					int len = 0;
					while((len = fileIn.read(buffer)) > 0) {
						fileOut.write(buffer, 0, len);
					}
					result = true;
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		return result;
	}
	
	
	public String albumList() {
		
		PhotoDAO photoDao = new PhotoImple();
		albumList = photoDao.getAlbumList();
		
		return SUCCESS;
	}
	
	public String photoList() {
		
		PhotoDAO photoDao = new PhotoImple();
		photoListThumb = photoDao.getPhotoList(album.getId());
		photo = new Photo();
		
		return SUCCESS;
	}
	
	public String photoById() {
		PhotoDAO photoDao = new PhotoImple();
		photoList = photoDao.getPhotoListById(photo.getId());
		return SUCCESS;
	}
	
	
	public String deletePhoto() {
		
		PhotoDAO photoDao = new PhotoImple();
		photoDao.deletePhoto(photo.getId());
		
		photoDao.getPhotoList(photo.getAlbumId());
		
		return SUCCESS;
	}

	public List<Album> getAlbumList() {
		return albumList;
	}

	public void setAlbumList(List<Album> albumList) {
		this.albumList = albumList;
	}

	public Photo getPhoto() {
		return photo;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	public Album getAlbum() {
		return album;
	}

	public void setAlbum(Album album) {
		this.album = album;
	}

	public List<Photo> getPhotoList() {
		return photoList;
	}

	public void setPhotoList(List<Photo> photoList) {
		this.photoList = photoList;
	}

	public String getSavePath() {
		String tempPath = ServletActionContext.getRequest().getRealPath(savePath);
		return tempPath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

//	public User getUser() {
//		return user;
//	}
//
//	public void setUser(User user) {
//		this.user = user;
//	}

	public int getAbleSize() {
		return ableSize;
	}

	public void setAbleSize(int ableSize) {
		this.ableSize = ableSize;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public List<File> getUpload() {
		return upload;
	}

	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public List<String> getUploadContentType() {
		return uploadContentTypes;
	}

	public void setUploadContentType(List<String> uploadContentTypes) {
		this.uploadContentTypes = uploadContentTypes;
	}

	public List<Photo> getPhotoListThumb() {
		return photoListThumb;
	}

	public void setPhotoListThumb(List<Photo> photoListThumb) {
		this.photoListThumb = photoListThumb;
	}
	
	
}
